<?php
session_start();
function randomText($length) {
    $pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
    for($i=0;$i<$length;$i++) {
      $key .= $pattern{rand(0,35)};
    }
    return $key;
}

$_SESSION['tmptxt'] = randomText(4);
$captcha = imagecreatefromgif("bgcaptcha.gif");
$colText = imagecolorallocate($captcha, 255, 255, 255);
$font = imageloadfont("font.gdf");
imagestring($captcha, $font, 25, 1, $_SESSION['tmptxt'], $colText);

header("Content-type: image/gif");
imagegif($captcha);
imagedestroy($captcha);
?>